# coding:utf-8
"""
description:scene、button和station 添加或修改task
"""
import os
import random
import time

from appium.webdriver.common import mobileby

from common.Gesture_Mainpulation import gesture_mainpulation
from common.Get_Yaml import get_yaml
from common.Log import LOG, logger
from config.GlobalParameter import TEST_Element_YAML
from pages.Devices.AddDevice_page import AddDevice_page

Station_Data = get_yaml(os.path.join(TEST_Element_YAML, 'Devices', 'HubAndStation.yaml'))


class HubAndStation_page(AddDevice_page):
    by = mobileby.MobileBy()

    """hub--------------------------------------------》connected Devices集合，获取文字、长度和点击"""
    HubConnDev_loc = (by.ID, Station_Data.get_elementInfo(0))

    def get_HubConnDev(self):
        """hub》connected Devices集合，获取文字、长度和点击"""
        return self.find_elements(*self.HubConnDev_loc)

    def func_HubConnDev(self):
        """
        断言Hub Connected Device数量是否相等
        :return:
        """
        AgoN = super().get_ConnectedDevices().text
        if AgoN == "0":
            LOG.info("测试Smart hub》Connected Devices数量为0，无法点击测试.将跳过此测试。。。")
            return True
        else:
            LOG.info("测试Smart hub》点击Connected Devices（s）,断言Connected Device数量是否相等。。。")
            super().get_ConnectedDevices().click()
            return int(AgoN) == len(self.get_HubConnDev())

    """station--------------------》station connected device页面中的Add IR　REMOTE CONTROL按钮"""
    StationAddDevice_loc = (by.ID, Station_Data.get_elementInfo(1))
    """ Add IR　Remote Control》3-step guide 弹框的start 按钮"""
    btnAddStart_loc = (by.ID, Station_Data.get_elementInfo(2))

    """Add IR　Remote Control》滑动add ir remote control弹框，显示所有brand name"""
    StationAddIr_loc = (by.ID, Station_Data.get_elementInfo(3))

    """Add IR　Remote Control》获取Control弹框的7个brand name 集合"""
    ConnectedApplianceName_loc = (by.ID, Station_Data.get_elementInfo(4))
    """ Add IR　Remote Control》点击brand后跳转列表页弹框的start 按钮"""
    StationAddIrStart_loc = (by.ID, Station_Data.get_elementInfo(5))

    """Add IR　Remote Control》获取每个brand list item name集合"""
    StationListViewItem_loc = (by.ID, Station_Data.get_elementInfo(6))
    """Add IR　Remote Control》滑动brand list item模块"""
    AvailableBrands_loc = (by.ID, Station_Data.get_elementInfo(7))

    def click_StationAddDevice(self):
        """station connected device页面中的Add IR　REMOTE CONTROL按钮"""
        self.find_element(*self.StationAddDevice_loc).click()

    def click_btnAddStart(self):
        """ Add IR　Remote Control》3-step guide 弹框的start 按钮"""
        self.find_element(*self.btnAddStart_loc).click()

    def swipe_StationAddIr(self):
        """Add IR　Remote Control》滑动add ir remote control弹框，显示所有brand name"""
        centre = gesture_mainpulation().centreXAndY(self.find_element(*self.StationAddIr_loc))
        self.driver.swipe(centre[0], centre[3] - 30, centre[0], centre[5] + 300, 200)  # 上滑，y大到小

    def get_ConnectedApplianceName(self):
        """Add IR　Remote Control》获取Control弹框的7个brand name 集合"""
        return self.find_elements(*self.ConnectedApplianceName_loc)

    def click_StationAddIrStart(self):
        """ Add IR　Remote Control》点击brand后跳转列表页弹框的start 按钮"""
        self.find_element(*self.StationAddIrStart_loc).click()

    def get_StationListViewItem(self):
        """Add IR　Remote Control》获取每个brand list item name集合"""
        return self.find_elements(*self.StationListViewItem_loc)

    def swipe_AvailableBrands(self):
        """Add IR　Remote Control》滑动brand list item模块"""
        time.sleep(5)
        centre = gesture_mainpulation().centreXAndY(self.find_element(*self.AvailableBrands_loc))
        self.driver.swipe(centre[0], centre[3] - 200, centre[0], centre[5] + 620, 200)  # 上滑，y大到小

    """Add IR　Remote Control》CUSTOMIZE REMOTE CONTROL按钮"""
    btnStationAddIrCustom_loc = (by.ID, Station_Data.get_elementInfo(12))

    def click_btnStationAddIrCustom(self):
        """Add IR　Remote Control》CUSTOMIZE REMOTE CONTROL按钮"""
        self.find_element(*self.btnStationAddIrCustom_loc).click()

    """Add IR　Remote Control》REMOTE CONTROL的按钮，集合.除了第一个！icon"""
    IRRemoteButtons_loc = (by.CLASS_NAME, Station_Data.get_elementInfo(13))

    def getAndClick_IRRemoteButtons(self, indexs=6):
        """Add IR　Remote Control》随机点击REMOTE CONTROL的按钮，集合.除了第一个！icon（索引号0）"""
        buttons = self.find_elements(*self.IRRemoteButtons_loc)
        del buttons[0]  # 删除集合的第一个！icon元素
        buttons[indexs].click()  # 根据索引号点击

    """Add IR　Remote Control》添加页面的no response按钮。顶部Control name序号有变动"""
    NoResponse_loc = (by.ID, Station_Data.get_elementInfo(14))

    def click_NoResponse(self):
        """Add IR　Remote Control》添加页面的no response按钮。顶部Control name序号有变动"""
        self.find_element(*self.NoResponse_loc).click()

    """Add IR　Remote Control》添加页面的Response按钮。text:RESPONSE (0/3)变动.
    点击完三个remote button后的DONE按钮"""
    Response_loc = (by.ID, Station_Data.get_elementInfo(15))

    def click_Response(self):
        """Add IR　Remote Control》添加页面的Response按钮。text:RESPONSE (0/3)变动.
        点击完三个remote button后的DONE按钮"""
        self.find_element(*self.Response_loc).click()

    def gettext_Response(self):
        """Add IR　Remote Control》获取添加页面的Response按钮。text:RESPONSE (0/3)变动."""
        return self.find_element(*self.Response_loc).text

    """Add IR　Remote Control》顶部Control name.text:AIRTEK Remote (1/24).
    显示添加成功后输入的name"""
    DeviceControlName_loc = (by.ID, Station_Data.get_elementInfo(16))

    def get_DeviceControlName(self):
        """Add IR　Remote Control》顶部Control name.text:AIRTEK Remote (1/24).
        显示添加成功后输入的name"""
        return self.find_element(*self.DeviceControlName_loc).text

    """Add IR　Remote Control》Name your remote，输入框。关闭弹框按钮见Public.yaml(3)"""
    etStationInput_loc = (by.ID, Station_Data.get_elementInfo(17))

    def input_etStationInput(self, StationIRName):
        """Add IR　Remote Control》Name your remote，输入框。关闭弹框按钮见Public.yaml(3)"""
        self.send_keys(StationIRName, self.find_element(*self.etStationInput_loc))

    """Add IR　Remote Control》Name your remote，next按钮"""
    btnStationInputNext_loc = (by.ID, Station_Data.get_elementInfo(18))

    def click_btnStationInputNext(self):
        """Add IR　Remote Control》Name your remote，next按钮"""
        self.find_element(*self.btnStationInputNext_loc).click()

    def func_addIRRemote_error(self, message):
        """
        Add IR　Remote Control按钮》进入remote control button page，断言response后的提示内容
        :param message:
        :return:
        """
        if self.assertVisible(*self.StationAddDevice_loc):
            LOG.info("根据Device Name进入到Smart Station，直接点击Add IR　REMOTE CONTROL按钮。。。")
            self.click_StationAddDevice()  # 点击station页面中的Add IR　REMOTE CONTROL按钮
        else:
            LOG.info("根据Device Name进入到Smart Station Lite>Connected Device(s)>点击Add IR　REMOTE CONTROL按钮。。。")
            super().click_btnBack()  # Smart Station Lite从Settings Page返回到dashboard页面
            super().Add_StationIR()
        self.click_btnAddStart()
        self.swipe_StationAddIr()  # 滑动后显示所有brand name
        random.choice(self.get_ConnectedApplianceName()).click()  # 随机选择brand name进行添加
        self.click_StationAddIrStart()  # 点击brand后跳转列表页弹框的start 按钮
        random.choice(self.get_StationListViewItem()).click()  # 随机选择brand list item
        self.getAndClick_IRRemoteButtons(-1)  # 点击最后一个按钮
        LOG.info("Response按钮文字变化，{}》》》".format(self.gettext_Response()))
        self.click_Response()
        return self.find_toast(message)

    def func_addIRRemote_error2(self, message):
        """
        remote control button page，断言重复点击已测试的button后的提示内容
        :param message:
        :return:
        """
        self.getAndClick_IRRemoteButtons(-1)  # 点击最后一个按钮
        return self.find_toast(message)

    def func_addIRRemote_error3(self, message):
        """
        remote control button page，断言点击No response后的提示内容
        如果没有下一个，断言提示内容
        :param message:
        :return:
        """
        ago = self.get_DeviceControlName()  # 获取顶部control name
        self.getAndClick_IRRemoteButtons()  # 点击其他按钮
        self.click_NoResponse()  # 点击No Response
        LOG.info("顶部Control name变化，{}》》》{}".format(ago, self.get_DeviceControlName()))
        if ago == self.get_DeviceControlName():
            return self.find_toast(message[0])  # 没有下一个remote control学习
        else:
            return self.find_toast(message[1])

    def func_addIRRemote(self, message, StationIRName):
        """
        成功添加station ir remote control，断言成功后的StationIRName
        :param message:
        :param StationIRName:
        :return:
        """
        for i in range(4):
            # 随机数要避开conditioner顶部不能点击的三个image 元素
            self.getAndClick_IRRemoteButtons(random.randint(4, 10))
            if self.gettext_Response() != "Done":
                self.getAndClick_IRRemoteButtons(random.randint(4, 10))
            LOG.info("Response按钮文字变化，{}》》》".format(self.gettext_Response()))
            self.click_Response()
        self.input_etStationInput(StationIRName)
        self.click_btnStationInputNext()
        self.find_toast(message)
        return self.get_DeviceControlName() == StationIRName

    # Station》connected Devices集合，获取文字长度和点击
    StationConnDev_loc = (by.ID, Station_Data.get_elementInfo(8))

    def get_StationConnDev(self):
        """Station》connected Devices集合，获取文字长度和点击"""
        return self.find_elements(*self.StationConnDev_loc)

    def func_StationConnDev(self):
        """测试Station Connected Device，断言数字相同
        :return:
        """
        AgoN = super().get_ConnectedDevices().text
        if AgoN == "0":
            LOG.info("测试Smart Station或者Station lite》Connected Devices数量为0，无法点击测试.将跳过此测试》》》")
            return True
        else:
            LOG.info("Smart Station或者Station lite》Connected Devices（s）,断言Connected Device数量是否相等。。。")
            super().get_ConnectedDevices().click()
            return int(AgoN) == len(self.get_StationConnDev())

    # Station Device Setting》Indicator Light一行
    StationBrightness_loc = (by.ID, Station_Data.get_elementInfo(9))

    def get_StationBrightness(self):
        """Station Device Setting》Indicator Light一行"""
        return self.find_element(*self.StationBrightness_loc)

    def func_StationBrightness(self):
        """
        测试Station Brightness，断言选择文字包含在Brightness里
        :return:
        """
        gesture_mainpulation().swipe_up(self.driver, self.get_DeviceSettingsPage())  # 滑动Setting page
        self.get_StationBrightness().click()
        super().swipe_NumberPicker_input()  # 滑动弹框
        AgoTe = super().get_NumberPicker_input()  # 获取Number Picker的文字做断言
        super().click_popup_NumberPicker_confirm()
        return AgoTe in self.get_StationBrightness().text

    # Station Device Setting》Slient mode 一行
    StationSilent_loc = (by.ID, Station_Data.get_elementInfo(10))

    def get_StationSilent(self):
        """Station Device Setting》Slient mode 一行"""
        return self.find_element(*self.StationSilent_loc)

    def func_StationSilent(self, Message):
        """
        测试Station Slient Mode
        :param Message: 断言内容
        :return:
        """
        self.get_StationSilent().click()
        if self.get_StationSilent().text == 'ON':
            LOG.info("测试Station Slient Mode为开启状态。。。")
            super().click_btnBack()
            return super().find_toast(Message)
        else:
            LOG.info("测试Station Slient Mode为关闭状态。。。")
            return True

    def func_SelectIRDev(self):
        """
        进入IR Remote Control Settings page
        :return:
        """
        if super().assertVisible(*super().ConnectedDevices_loc):
            # 如果直接根据IR　Control　name进入setting page，则不用点击Station Conn Device后续操作
            if super().get_ConnectedDevices().text == "0":
                LOG.info("测试IR Control》根据Station Name为入口，Connected Devices数量为0，无法点击测试。。。")
            else:
                LOG.info("测试IR Control》点击Station Connected Devices跳转页面测试。。。》》》")
                super().get_ConnectedDevices().click()
                random.choice(self.get_StationConnDev()).click()
                super().click_ControlSettings()

    # IR Control Device Setting》获取Conn Device文字，其他修改name，location，remove元素见Public.yaml
    SettingsStationName_loc = (by.ID, Station_Data.get_elementInfo(11))

    def get_SettingsStationName(self):
        """IR Control Device Setting》获取Conn Device文字"""
        return self.find_element(*self.SettingsStationName_loc).text

    def Func_ConnectedDevice(self):
        """
        Device Setting》there sensor，button，switches和dimmer设备获取Connected hub
        :return:
        """
        if self.assertVisible(*self.SettingsStationName_loc):
            LOG.info("{}连接的Station是：{}>>>".format(super().get_SettingsName(), self.get_SettingsStationName()))
